package com.jason.library.delegate.standard

import kotlin.properties.Delegates

/**
 * Kotlin 标准库中提供几个委托
 */
object StandardMain{

    @JvmStatic
    fun main(args : Array<String>) {
        lazyProperty()
        observableProperty()
        vetoableProperty()
    }
}

/**
 * 延迟属性 lazy
 */
fun lazyProperty() {
    val lazyPro : String by lazy {
        println("Hello，第一次调用才会执行我！")
        "西蒙"
    }

    println(lazyPro)
    println(lazyPro)
    println(lazyPro)
}

/**
 * 可观察属性 Observable
 */
fun observableProperty() {
    var observablePro : String by Delegates.observable("默认值：jason") {
        property, oldValue, newValue ->  println("property: $property: $oldValue -> $newValue ")
    }

    observablePro = "第一次修改值";
    observablePro = "第二次修改值";
}

/**
 * 可观察条件属性 vetoable 满足条件，赋值新值，不满足，还是旧值
 */
fun vetoableProperty() {
    var vetoablePro : Int by Delegates.vetoable(10) {
        property, oldValue, newValue ->  newValue > oldValue
    }

    println("vetoableProp=$vetoablePro")
    vetoablePro = 10
    println("vetoableProp=$vetoablePro")
    vetoablePro = 11
    println("vetoableProp=$vetoablePro")
    vetoablePro = 9
    println("vetoableProp=$vetoablePro")
    vetoablePro = 100
    println("vetoableProp=$vetoablePro")
}

fun mapProperty() {
    val user = User(mapOf(
        "name" to "西蒙",
        "age" to 25
    ))

    println("name=${user.name} age=${user.age}")
}